<?php

namespace app\command;

use app\common\model\CommissionLog;
use app\common\model\Merchant;
use app\common\model\WithdrawOrder;
use think\console\Command;

class GenerateWithdraw extends Command
{

    protected function configure()
    {
        $this->setName('g_withdraw');
    }

    protected function execute(\think\console\Input $input, \think\console\Output $output)
    {

        for ($j = 1; $j < 100 ; $j++) {
            for ($i = 1; $i < 4 ; $i++) {
                $time = strtotime('2025-'.$i.'-'.mt_rand(1,30));

                $merchant = Merchant::where('commission','>',100)->orderRand()->findOrFail();
                /**
                 * @var WithdrawOrder $withdrawOrder
                 */
                $withdrawOrder = new WithdrawOrder();
                $withdrawOrder->merchant_id = $merchant->id;
                $withdrawOrder->amount = mt_rand(0,$merchant->commission) ;
                $withdrawOrder->sn = generate_sn(WithdrawOrder::class, 'sn','w',4,'', $time);
                $withdrawOrder->create_time =$time;
                $withdrawOrder->account = $this->generatePhone() ;
                $withdrawOrder->way =2;
                $withdrawOrder->save();

                CommissionLog::log($merchant, -$withdrawOrder->amount , '提现申请'.$withdrawOrder->sn);
            }
        }



    }


    private function generatePhone()
    {
        $prefixes = [
            '130', '131', '132', '145', '155', '156', '166', '171', '175', '176', '185', '186', '196', // 联通
            '134', '135', '136', '137', '138', '139', '147', '150', '151', '152', '157', '158', '159', '172', '178', '182', '183', '184', '187', '188', '195', '197', '198', // 移动
            '133', '149', '153', '173', '174', '177', '180', '181', '189', '190', '191', '193', '199' // 电信
        ];
        // 随机选择一个号段
        $prefix = $prefixes[array_rand($prefixes)];
        // 随机生成后 8 位数字
        $suffix = str_pad(mt_rand(0, 99999999), 8, '0', STR_PAD_LEFT);
        // 拼接手机号
        return $prefix . $suffix;

    }


    private function generateRandName()
    {
        $surnames = ['赵', '钱', '孙', '李', '周', '吴', '郑', '王', '冯', '陈', '褚', '卫', '蒋', '沈', '韩', '杨'];
        // 定义常见名字数组
        $names = ['伟', '芳', '娜', '敏', '静', '强', '磊', '军', '洋', '勇', '艳', '杰', '娟', '涛', '明', '超'];
        // 随机选择一个姓氏
        $surname = $surnames[array_rand($surnames)];
        // 随机决定名字长度（1 或 2 个字）
        $nameLength = rand(1, 2);
        $name = '';
        if ($nameLength === 1) {
            // 随机选择一个名字
            $name = $names[array_rand($names)];
        } else {
            // 随机选择两个名字
            $name = $names[array_rand($names)] . $names[array_rand($names)];
        }
        return $surname. $name;

    }
}